package com.liwei.lwmail.ware.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class WareRabbitMQConfig {

//    @RabbitListener(queues = {"stock.release.queue"})
//    public void listener(Message message){
//        System.out.println(message.getMessageProperties().toString());
//    }

    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 库存交换器 stock.event.exchange
     */
    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange("stock.event.exchange",true,false);
    }

    /**
     * 队列stock.release.queue
     */
    @Bean
    public Queue stockReleaseQueue(){
        return new Queue("stock.release.queue",true,false,false);
    }

    /**
     * 延时队列stock.delay.queue
     */
    @Bean
    public Queue stockDelayQueue(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange","stock.event.exchange");
        arguments.put("x-dead-letter-routing-key","stock.release");
        arguments.put("x-message-ttl",120000);
        return new Queue("stock.delay.queue",true,false,false, arguments);
    }

    /**
     * 绑定关系
     */
    @Bean
    public Binding bindingReleaseQueue(){
        return new Binding("stock.release.queue",
                Binding.DestinationType.QUEUE,
                "stock.event.exchange",
                "stock.release.#",
                null);
    }
    /**
     * 绑定关系
     */
    @Bean
    public Binding bindingDelayQueue(){
        return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock.event.exchange",
                "stock.locked",
                null);
    }

}
